package com.lmscn.lms.api.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

import java.util.Date;
import java.util.List;

/**
 * Created by xiaojie on 15-11-12.
 */
public class LessonStudentRecord extends Model<LessonStudentRecord> {

    public static final LessonStudentRecord dao = new LessonStudentRecord();

    public LessonStudentRecord findByCourseIdAndStudentId(Long courseId, Long studentId){
        return dao.findFirst("select * from t_lesson_student_record " +
                "where course_id = ? and student_id = ? order by learned_starttime desc", courseId, studentId);
    }

    public void save(LessonStudentRecord lessonStudentRecord){
        lessonStudentRecord.remove("id").save();
    }

    public List<LessonStudentRecord> findByStartTime(Long studentId,Date beginDate,Date endDate){
        return dao.find("select * from t_lesson_student_record " +
                "where student_id = ? and  learned_starttime >=? and learned_starttime <=?",studentId,beginDate,endDate);
    }
    public List<LessonStudentRecord> findByStartTimeToNumber(Long studentId,Date beginDate,Date endDate){
        return dao.find("select * from t_lesson_student_record " +
                "where student_id = ? and learned_starttime >=? and learned_starttime <=? group by course_id",studentId,beginDate,endDate);
    }
    
    /**
     * 删除课件学员信息
     * @param courseStudentId
     * @param courseId
     * @param studentId
     * @param lessonId
     */
    public void deleteLessonStudentRecord(Long courseStudentId, Long courseId, Long studentId, Long lessonId) {
        Db.update("delete from t_lesson_student_record " +
                "where course_student_id =? and course_id=? and student_id=? and lesson_id=?",courseStudentId,courseId,studentId,lessonId);
    }
}
